import request from '@/utils/request'

// 查询列表
export const listINFO = (query:any) => {
  return request({
    url: '/report/kpi/machineFrencyList',
    method: 'get',
    params: query
  })
}

export const getParentInfo = (query:any) => {
  return request({
    url: '/interface/api/machineKpiReportList',
    method: 'get',
    params: query
  })
}

type Obj = Record<string, any>

export const _trendsTitle:string[] = ['时间稼动率(%)', '总DOWN机率(%)', '生产PM率(%)', '设备PM率(%)', '其他PM率(%)', '待机率(%)']

const cellStyle = ({ rowIndex, data, value, colDef }: Obj) => {
  if(rowIndex === 0){
    if(_trendsTitle.includes(value)){
      return {
        background: '#fce4d6'
      }
    } else if(colDef.field !== 'attribute15') {
      return {
        color: 'blue',
        fontWeight: 'bold',
        textDecoration: 'underline',
        cursor: 'pointer'
      }
    }
  }

  if(data.title === '实绩-目标' && Number(value) < 0){
    return {
      color: 'red',
      fontWeight: 'bold'
    }
  }

}

const cellStyleActual = ({ rowIndex, data, value, colDef }: Obj) => {
  if(rowIndex === 0){
    if(_trendsTitle.includes(value)){
      return {
        background: '#fce4d6'
      }
    } else if(colDef.field !== 'attribute15') {
      return {
        color: 'blue',
        fontWeight: 'bold',
        textDecoration: 'underline',
        cursor: 'pointer'
      }
    }
  }

  if(rowIndex === 2){
    if(colDef.field !== 'attribute15' && colDef.field !== 'title') {
      return {
        color: 'blue',
        fontWeight: 'bold',
        textDecoration: 'underline',
        cursor: 'pointer'
      }
    }
  }

  if(data.title === '实绩-目标' && Number(value) < 0){
    return {
      color: 'red',
      fontWeight: 'bold'
    }
  }

}

export const columns = [
  { headerName: "title", field: "title", minWidth: 140, cellStyle },
  { headerName: "attribute15", field: "attribute15", minWidth: 55, cellStyle },
  { headerName: "attribute1", field: "attribute1", minWidth: 55, cellStyle },
  { headerName: "attribute2", field: "attribute2", minWidth: 55, cellStyle },
  { headerName: "attribute3", field: "attribute3", minWidth: 55, cellStyle },
  { headerName: "attribute4", field: "attribute4", minWidth: 55, cellStyle },
  { headerName: "attribute5", field: "attribute5", minWidth: 55, cellStyle },
  { headerName: "attribute6", field: "attribute6", minWidth: 55, cellStyle },
  { headerName: "attribute7", field: "attribute7", minWidth: 55, cellStyle },
  { headerName: "attribute8", field: "attribute8", minWidth: 55, cellStyle },
  { headerName: "attribute9", field: "attribute9", minWidth: 55, cellStyle },
  { headerName: "attribute10", field: "attribute10", minWidth: 55, cellStyle },
  { headerName: "attribute11", field: "attribute11", minWidth: 55, cellStyle },
  { headerName: "attribute12", field: "attribute12", minWidth: 55, cellStyle },
  { headerName: "attribute13", field: "attribute13", minWidth: 55, cellStyle },
  { headerName: "attribute14", field: "attribute14", minWidth: 55, cellStyle }
]

export const actualColumns = [
  { headerName: "title", field: "title", minWidth: 140, cellStyle: cellStyleActual },
  { headerName: "attribute15", field: "attribute15", minWidth: 55, cellStyle: cellStyleActual },
  { headerName: "attribute1", field: "attribute1", minWidth: 55, cellStyle: cellStyleActual },
  { headerName: "attribute2", field: "attribute2", minWidth: 55, cellStyle: cellStyleActual },
  { headerName: "attribute3", field: "attribute3", minWidth: 55, cellStyle: cellStyleActual },
  { headerName: "attribute4", field: "attribute4", minWidth: 55, cellStyle: cellStyleActual },
  { headerName: "attribute5", field: "attribute5", minWidth: 55, cellStyle: cellStyleActual },
  { headerName: "attribute6", field: "attribute6", minWidth: 55, cellStyle: cellStyleActual },
  { headerName: "attribute7", field: "attribute7", minWidth: 55, cellStyle: cellStyleActual },
  { headerName: "attribute8", field: "attribute8", minWidth: 55, cellStyle: cellStyleActual },
  { headerName: "attribute9", field: "attribute9", minWidth: 55, cellStyle: cellStyleActual },
  { headerName: "attribute10", field: "attribute10", minWidth: 55, cellStyle: cellStyleActual },
  { headerName: "attribute11", field: "attribute11", minWidth: 55, cellStyle: cellStyleActual },
  { headerName: "attribute12", field: "attribute12", minWidth: 55, cellStyle: cellStyleActual },
  { headerName: "attribute13", field: "attribute13", minWidth: 55, cellStyle: cellStyleActual },
  { headerName: "attribute14", field: "attribute14", minWidth: 55, cellStyle: cellStyleActual }
]

export const getTableData = (data:any[]) => {

  let Maps:Obj = {
    'B': new Map([
      [1, "时间稼动率(%)"],
      [2, "目标"],
      [3, "实绩"],
      [4, "实绩-目标"],
      [5, "总时间损失率(%)"],
      [6, "保养(PM)"],
      [7, "故障(Down)"],
      [8, "待机(IDLE)"],
      [9, "无计划(IDLE)"],
      [10, "离线(OFF)"]
    ]),
    'C': new Map([
      [1, "总DOWN机率(%)"],
      [2, "目标"],
      [3, "实绩"],
      [4, "实绩-目标"],
      [5, "设备故障率"],
      [6, "Interlock率"],
      [7, "原材料故障率"],
      [8, "辅材料故障率"]
    ]),
    'D': new Map([
      [1, "生产PM率(%)"],
      [2, "目标"],
      [3, "实绩"],
      [4, "实绩-目标"],
      [5, "应急报警率"],
      [6, "生产5S率"],
      [7, "一级保养率"]
    ]),
    'E': new Map([
      [1, "设备PM率(%)"],
      [2, "目标"],
      [3, "实绩"],
      [4, "实绩-目标"],
      [5, "定期PM率"],
      [6, "设备非定期率"],
      [7, "品质非定期率"]
    ]),
    'F': new Map([
      [1, "其他PM率(%)"],
      [2, "目标"],
      [3, "实绩"],
      [4, "实绩-目标"],
      [5, "实验借机率"],
      [6, "改造改善率"],
      [7, "辅材变更率"],
      [8, "版型变更率"],
      [9, "异常PM率"]
    ]),
    'G': new Map([
      [1, "待机率(%)"],
      [2, "目标"],
      [3, "实绩"],
      [4, "实绩-目标"],
      [5, "待料率"],
      [6, "堵料率"]
    ])
  }

  let targetData:Obj = {}
  let jump = null
  let objKeys:string[] = Object.keys(data)

  Object.values(data).forEach((val:Obj, index:number, arr:Obj) => {
    targetData[objKeys[index]] = {}
    targetData[objKeys[index]].tableData = []
    val.forEach((item:any, idx:number) => {

      if(index === 0 && idx === 0){
        jump = item
      }

      if(idx !== 0){
          item.title = Maps[objKeys[index]].get(idx)
        targetData[objKeys[index]].tableData.push(item)
      }
    })
  })

  return {
    targetData,
    jump
  }
}

export const chartOptios = (data:any[], order:number, time:string, device:string) => {
  let legendData:string[] = ['实绩-目标']
  let xAxisData:string[] = []

  let _dataPool:Obj = {}
  data.forEach((val:Obj[], index:number) => {
    if(index === 0){
      let _obj = val[0]

      xAxisData = [_obj.attribute15, _obj.attribute1, _obj.attribute2, _obj.attribute3, _obj.attribute4,
                  _obj.attribute5, _obj.attribute6, _obj.attribute7, _obj.attribute8, _obj.attribute9,
                  _obj.attribute10, _obj.attribute11, _obj.attribute12, _obj.attribute13, _obj.attribute14]
    }
  })
  let _tempxAxisData = ['attribute15', 'attribute1', 'attribute2', 'attribute3', 'attribute4',
    'attribute5', 'attribute6', 'attribute7', 'attribute8', 'attribute9',
    'attribute10', 'attribute11', 'attribute12', 'attribute13', 'attribute14']

    let _barData = [
      '保养(PM)', '故障(Down)', '待机(IDLE)', '无计划(IDLE)', '离线(OFF)',
      '设备故障率', '设备故障率', 'Interlock率', '原材料故障率', '辅材料故障率',
      '应急报警率', '生产5S率', '一级保养率',
      '定期PM率', '设备非定期率', '品质非定期率',
      '实验借机率', '改造改善率', '辅材变更率', '版型变更率', '异常PM率',
      '待料率', '堵料率'
    ]

    let _lineData = ['目标', '实绩', '总时间损失率(%)']

  let options:any = {
    title: {
      text: '',
      textStyle: {
        fontSize:'16px',
        fontWeight: 500
      }
    },
    legend: {
      right: '30',
      data: legendData
    },
    toolbox: {
      feature: {
        saveAsImage: {},
        brush: {
          type: ['']
        }
      }
    },
    grid: [
      {
        left: '140px',
        right: '4px',
        height: '14%',
        top:'18%'
      },
      {
        left: '140px',
        right: '4px',
        top: '40%',
        bottom: '10px'
      }
    ],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross'
      },
      borderWidth: 1,
      borderColor: '#ccc',
      padding: 10,
      textStyle: {
        color: '#000',
        fontSize: 12
      }
    },
    xAxis: [
      {
        type: 'category',
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: false // 不显示坐标轴线
        },
        axisLabel: {
          show: false // 不显示坐标轴上的文字
        },
        data: xAxisData // 横坐标的数据
      },
      {
        gridIndex: 1,
        type: 'category',
        data: xAxisData,
        boundaryGap: true,
        axisLine: { onZero: false },
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLabel: {
          show: false // 不显示坐标轴上的文字
        }
        // min: 'dataMin',
        // max: 'dataMax'
      }
    ],
    yAxis: [
      {
        type: 'value',
        axisLabel: {
          textStyle: {
            color: '#fff'
          }
        },
        splitLine: {
          show: false
        },
        axisLine: {
          show: false
        },
        scale: true
        // min: 0
      },
      {
        gridIndex: 1,
        type: 'value',
        axisLabel: {
          textStyle: {
            color: '#fff'
          }
        },
        splitLine: {
          show: false
        },
        axisLine: {
          show: false
        },
        scale: true
      }
    ],
    series: []
  }

  data[order].forEach((item:Obj, idx:number) => {

    if(idx === 0){
      options.title.text = device /* 设备名 */ + '_' + item.title + '_' + time
    }

    if(item.title === '总时间损失率(%)' || idx !== 0){
      _dataPool[item.title] = _tempxAxisData.map(key => item[key])
    }

    if(_barData.includes(item.title) || _lineData.includes(item.title)){
      legendData.push(item.title)
    }

  })

  for(let key in _dataPool) {

      if(_barData.includes(key)){
        options.series.push({
          name: key,
          type: 'bar',
          stack: '总量',
          barWidth: '40%',
          xAxisIndex: 1,
          yAxisIndex: 1,
          // label: {
          //   show: true,
          //   position: 'insideBottom'
          // },
          data: _dataPool[key]
        })
      }

      if(_lineData.includes(key)){
        options.series.push({
          name: key,
          type: 'line',
          smooth: true,
          label: {
            show: key === '目标' ? false : true,
            position: 'top'
          },
          lineStyle: {
            width: 1
          },
          symbolSize: 2,
          xAxisIndex: 1,
          yAxisIndex: 1,
          data: _dataPool[key]
        })
      }

      if(key === '实绩-目标'){
        options.series.push({
          name: key,
          type: 'bar',
          barWidth: '40%',
          label: {
            show: true,
            position: 'top'
          },
          itemStyle: {
            color: '#dbdbdb'
          },
          data: _dataPool[key]
        })
      }
    }

  return options
}